# _*_ coding:UTF-8 _*_
__author__ = 'shede333'

import common_util
import os
import copy


def _file_list_convert_to_dic(file_list, root_dir_path=""):
    tmp_dic = {}
    if not root_dir_path.endswith("/"):
        root_dir_path += "/"
    dir_path_length = len(root_dir_path)
    for tmp_name, tmp_path in file_list:
        tmp_dic[tmp_path[dir_path_length:]] = tmp_name
    return tmp_dic


def _file_list_convert_to_dic_md5(file_list, root_dir_path=""):
    tmp_dic = {}
    if not root_dir_path.endswith("/"):
        root_dir_path += "/"
    dir_path_length = len(root_dir_path)
    for tmp_name, tmp_path in file_list:
        tmp_dic[common_util.file_md5(tmp_path)] = (tmp_name, tmp_path[dir_path_length:], tmp_path)
    return tmp_dic


def _print_list_md5(tmp_list, prefix_str=""):
    for element_0, element_1 in tmp_list:
        tmp_size = os.path.getsize(element_1) / (1024.0 ** 2)
        if tmp_size > 1:
            print prefix_str, element_0, " :%.3fMB" % tmp_size


def compare_dir_file_name_diff(dir_path_0, dir_path_1):
    """
    以文件名作为key,对比两个目录里文件的差异详情
    :param dir_path_0: 目录路径
    :param dir_path_1: 目录路径
    :return:
    """
    file_list_0 = common_util.file_list_in_dir(dir_path_0, is_recursion=True)
    file_dic_0 = _file_list_convert_to_dic(file_list_0, dir_path_0)
    file_list_1 = common_util.file_list_in_dir(dir_path_1, is_recursion=True)
    file_dic_1 = _file_list_convert_to_dic(file_list_1, dir_path_1)

    file_only_in_1 = []
    file_share = []

    tmp_dic = copy.copy(file_dic_0)
    for tmp_r_path in file_dic_1:
        if tmp_r_path in tmp_dic:
            file_share.append(tmp_r_path)
            del (tmp_dic[tmp_r_path])
        else:
            file_only_in_1.append(tmp_r_path)
    file_only_in_0 = tmp_dic.keys()

    print "\n%s 特有的文件:\n" % dir_path_0
    common_util.print_list(file_only_in_0, prefix_str="  ")

    print "\n%s 特有的文件:\n" % dir_path_1
    common_util.print_list(file_only_in_1, prefix_str="  ")

    print "\n二者共有的文件:\n"
    common_util.print_list(file_share, prefix_str="  ")


def compare_dir_file_md5_diff(dir_path_0, dir_path_1):
    """
    以文件md5作为key,对比两个目录里文件的差异详情
    :param dir_path_0: 目录路径
    :param dir_path_1: 目录路径
    :return:
    """
    file_list_0 = common_util.file_list_in_dir(dir_path_0, is_recursion=True)
    file_dic_0 = _file_list_convert_to_dic_md5(file_list_0, dir_path_0)
    file_list_1 = common_util.file_list_in_dir(dir_path_1, is_recursion=True)
    file_dic_1 = _file_list_convert_to_dic_md5(file_list_1, dir_path_1)

    file_only_in_0 = []
    file_only_in_1 = []
    file_share = []

    tmp_dic = copy.copy(file_dic_0)
    for tmp_f_md5 in file_dic_1:
        if tmp_f_md5 in tmp_dic:
            file_share.append((file_dic_1[tmp_f_md5][1], file_dic_1[tmp_f_md5][2]))
            del (tmp_dic[tmp_f_md5])
        else:
            file_only_in_1.append((file_dic_1[tmp_f_md5][1], file_dic_1[tmp_f_md5][2]))

    for tmp_f_md5 in tmp_dic:
        file_only_in_0.append((tmp_dic[tmp_f_md5][1], tmp_dic[tmp_f_md5][2]))

    print "\n%s 特有的文件:\n" % dir_path_0
    _print_list_md5(file_only_in_0, prefix_str="  ")

    print "\n%s 特有的文件:\n" % dir_path_1
    _print_list_md5(file_only_in_1, prefix_str="  ")

    # print "\n二者共有的文件:\n"
    # _print_list_md5(file_share, prefix_str="  ")


def main():
    """
    主入口
    :return:
    """
    # dir_0 = common_util.user_input_path(is_dir=True)
    # dir_1 = common_util.user_input_path(is_dir=True)
    # compare_dir_file_name_diff(dir_0, dir_1)

    dir_c = "/Users/baidu/Desktop/center_1EA426F2-CCE8-4BAE-AB83-C8DFA6E5FDC6/Documents"
    dir_0 = "/Users/baidu/Desktop/58F5A1B3-D65A-4455-B736-693A4A6C06F6/Documents"
    dir_1 = "/Users/baidu/Library/Developer/CoreSimulator/Devices/7498957D-8DF4-4D1E-A7F8-A5D4DFD470A1/data/Applications/58F5A1B3-D65A-4455-B736-693A4A6C06F6/Documents"
    compare_dir_file_md5_diff(dir_0, dir_1)
    # print "\n\n"
    # compare_dir_file_md5_diff(dir_0, dir_c)
    # print "\n\n"
    # compare_dir_file_md5_diff(dir_c, dir_1)


if __name__ == '__main__':
    main()
